package com.zj.study.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author 赵赳
 * @since 2021/12/11 15:26
 */
@Slf4j
@Aspect
@Component
public class LogAopConfig {

  @Pointcut("@annotation(com.zj.study.annotation.BeanLog)")
  public void BrokerAspect() {
  }

  @Pointcut("@annotation(com.zj.study.annotation.AutoConfigurationLog)")
  public void autoConfigAspect() {
  }

  /**
   * 自动配置类监控
   *
   * @param joinPoint joinPoint
   * @return null
   */
  @Around(value = "autoConfigAspect()")
  public Object autoconfigRound(ProceedingJoinPoint joinPoint) {
    Object target = null;
    try {
      target = joinPoint.proceed();
      var declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
      log.debug("{} 自动化配置成功", declaringTypeName);
    } catch (Throwable throwable) {
      throwable.printStackTrace();
    }
    return target;
  }

  /**
   * 监控bean初始化aop
   *
   * @param joinPoint joinPoint
   * @return null
   */
  @Around(value = "BrokerAspect()")
  public Object doAround(ProceedingJoinPoint joinPoint) {
    Object proceed = null;
    try {
      proceed = joinPoint.proceed();
      var name = joinPoint.getSignature().getName();
      var args = joinPoint.getArgs();
      var declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
      log.debug("Bean {} 初始化完成,参数:{}, 初始化bean目标类:{} ", name, args,
          declaringTypeName);
    } catch (Throwable throwable) {
      throwable.printStackTrace();
    }
    return proceed;
  }


}
